﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.CustomQuery
{
    public partial class CustomQueryDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                //todo 设置页面控件信息

                int uid = 0;
                Int32.TryParse(Request["uid"],out uid);

                SysCustomQuery item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (uid > 0)
                    {
                        item = session.Get<SysCustomQuery>(uid);
                    }

                    if (item != null)
                    {
                        //保存uid
                        ViewState["uid"] = uid;
                    }
                    else
                    {
                        item = new SysCustomQuery();
                    }
                    JGridParam.Value = item.ParamList;
                    JGridResult.Value = item.ResultList;
                    name.Text = item.Name;
                    sql.Text = item.Sql;
                }
                
                //在此设置控件的初始属性
                
                ListItemCollection list = new ListItemCollection();

                list.Add(new ListItem() { Text = "编号（对应sql）", Value = "Code" });
                list.Add(new ListItem() { Text = "名称（用于展示）", Value = "Text" });
                list.Add(new ListItem() { Text = "格式化取值", Value = "Format" });
                list.Add(new ListItem() { Text = "值说明", Value = "Memo" });
                JGridResult.Columns = list;

                list.Add(new ListItem() { Text = "值类型", Value = "PropType" });
                list.Add(new ListItem() { Text = "比较方式", Value = "CompaireType" });
                JGridParam.Columns = list;
            }
            else
            {
                //todo 保存数据
                
                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                SysCustomQuery item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (uid > 0)
                    {
                        item = session.Get<SysCustomQuery>(uid);
                    }
                    if (item == null)
                    {
                        item = new SysCustomQuery();
                    }

                    //在此设置item的各个属性
                    item.Name = name.Text;
                    item.Sql = sql.Text;
                    item.ResultList = JGridResult.Value;
                    item.ParamList = JGridParam.Value;

                    //保存并更新item
                    session.SaveOrUpdate(item);
                    session.Flush();
                }


                //框架返回成功提示
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/CustomQuery/CustomQueryDetail.aspx?tabid=CustomQueryDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + item.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}